/**************************************************************************//**
 * @item     CosyOS Kernel
 * @file     sv_int_loc.c
 * @brief    中断同步服务（仅在用户中断中调用，并在本地执行）
 * @author   迟凯峰
 * @version  V3.3.0
 * @date     2023.07.15
 ******************************************************************************/

#include "os_var.h"
#include "os_api.h"
#include "os_iss.h"



/*
 * 相对寄存器访问
 */
#if SYSCFG_MCUCORE == 8051
#pragma NOAREGS
#endif



/*
 * 飞信
 */
#if SYSCFG_FETION == __ENABLED__
/* 接收飞信 */
tFetion __recv_fetion__(tFetion _XDATA_MEM_ *tbox)
{
	tFetion tion = *tbox;
	*tbox = false;
	return tion;
}

#endif



/*
 * 消息邮箱
 */
#if SYSCFG_MAILBOX == __ENABLED__
/* 接收邮件 */
#if SYSCFG_MCUCORE == 8051

bool __recv_mail1__(void _XDATA_MEM_ *gm)
{
	#if SYSCFG_INTRECVMAIL == 0
	if(os_sign_write_mbx_write || vMbxNode == NULL || vMbxNode != gm) return true;
	#elif SYSCFG_INTRECVMAIL == 1
	if(os_sign_write_mbx_write || vMbxNode == NULL || vMbxNode->gmbx != gm) return true;
	#endif
	return false;
}

#if SYSCFG_INTRECVMAIL == 1

bool __recv_mail2__(void)
{
	if(!vMbxNode->lnmf) return false;
	vMbxNode->lnmf = false;
	return true;
}

void *__recv_mail3__(void)
{
	return vMbxNode->lmbx;
}

#endif

#else

bool __recv_mail0__(void *lm, void *gm, size_t size, bool *gf) __251_REENTRANT__
{
	#if SYSCFG_INTRECVMAIL == 0
	if(vMbxNode == NULL || vMbxNode != gm)
	#elif SYSCFG_INTRECVMAIL == 1
	if(vMbxNode == NULL || vMbxNode->gmbx != gm)
	#endif
	{
		if(*gf)
		{
			*gf = false;
			__memcpy(lm, gm, size);
			return true;
		}
		return false;
	}
	#if SYSCFG_INTRECVMAIL == 1
	else if(vMbxNode->lnmf)
	{
		vMbxNode->lnmf = false;
		__memcpy(lm, vMbxNode->lmbx, size);
		return true;
	}
	#endif
	return false;
}

#endif

#endif



/*
 * 消息队列
 */
#if SYSCFG_MSGQUEUE == __ENABLED__
/* 接收消息 */
#if SYSCFG_MCUCORE == 8051

static void *_recv_msg_(bool _XDATA_MEM_ *mut, void *msg)
{
	*mut = true;
	return msg;
}

bool __recv_msg1__(tspMsgQueue msg_queue)
{
	if(!msg_queue->mutex) goto __RET_FALSE;
	msg_queue->mutex = false;
	if(msg_queue->type == __DYNAMIC__)
	{
		#if SYSCFG_DEBUGGING == __ENABLED__
		vFault.error_recvmsg = true;
		#endif
		goto __FREE_MUTEX;
	}
	if(!msg_queue->counter) goto __FREE_MUTEX;
	return true;
	
__FREE_MUTEX:
	msg_queue->mutex = true;
	
__RET_FALSE:
	return false;
}

void *__recv_msg2__(tspMsgQueue msg_queue)
{
	void *msg;
	sRecvMsg_Static(msg);
	return _recv_msg_((bool *)&msg_queue->mutex, msg);
}

#else

void *__recv_msg0__(tspMsgQueue msg_queue)
{
	void *msg = NULL;
	if(!msg_queue->mutex) return NULL;
	msg_queue->mutex = false;
	if(msg_queue->type == __DYNAMIC__)
	{
		#if SYSCFG_DEBUGGING == __ENABLED__
		vFault.error_recvmsg = true;
		#endif
	}
	else if(!msg_queue->counter);
	else
	{
		sRecvMsg_Static(msg);
	}
	msg_queue->mutex = true;
	return msg;
}

#endif

#endif



/*
 * 全局变量
 */
#if SYSCFG_MCUCORE != 8051
/* 读全局变量 */
void *__read_gvar__(void *gp)
{
	if(vVarNode == NULL || vVarNode->gvar != gp) return gp;
	return vVarNode->lvar;
}

#endif
